<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>itemCreation.js 示例文件 | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-dynamicscene-example.html">QML Example - Dynamic Scene</a></td><td >itemCreation.js 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">itemCreation.js 示例文件</h1>
<span class="subtitle">dynamicscene/content/itemCreation.js</span>
<!-- $$$dynamicscene/content/itemCreation.js-description -->
<div class="descr"> <a name="details"></a>
<pre class="js">

  var itemComponent = <span class="number">null</span>;
  var draggedItem = <span class="number">null</span>;
  var startingMouse;
  var posnInWindow;

  <span class="keyword">function</span> <span class="name">startDrag</span>(mouse)
  {
      <span class="name">posnInWindow</span> <span class="operator">=</span> <span class="name">paletteItem</span>.<span class="name">mapToItem</span>(<span class="name">window</span>, <span class="number">0</span>, <span class="number">0</span>);
      <span class="name">startingMouse</span> <span class="operator">=</span> { x: <span class="name">mouse</span>.<span class="name">x</span>, y: <span class="name">mouse</span>.<span class="name">y</span> }
      <span class="name">loadComponent</span>();
  }

  <span class="comment">//Creation is split into two functions due to an asynchronous wait while</span>
  <span class="comment">//possible external files are loaded.</span>

  <span class="keyword">function</span> <span class="name">loadComponent</span>() {
      <span class="keyword">if</span> (<span class="name">itemComponent</span> <span class="operator">!=</span> <span class="number">null</span>) { <span class="comment">// component has been previously loaded</span>
          <span class="name">createItem</span>();
          <span class="keyword">return</span>;
      }

      <span class="name">itemComponent</span> <span class="operator">=</span> <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="name">paletteItem</span>.<span class="name">componentFile</span>);
      <span class="keyword">if</span> (<span class="name">itemComponent</span>.<span class="name">status</span> <span class="operator">==</span> <span class="name">Component</span>.<span class="name">Loading</span>)  <span class="comment">//Depending on the content, it can be ready or error immediately</span>
          <span class="name">component</span>.<span class="name">statusChanged</span>.<span class="name">connect</span>(<span class="name">createItem</span>);
      <span class="keyword">else</span>
          <span class="name">createItem</span>();
  }

  <span class="keyword">function</span> <span class="name">createItem</span>() {
      <span class="keyword">if</span> (<span class="name">itemComponent</span>.<span class="name">status</span> <span class="operator">==</span> <span class="name">Component</span>.<span class="name">Ready</span> <span class="operator">&amp;&amp;</span> <span class="name">draggedItem</span> <span class="operator">==</span> <span class="number">null</span>) {
          <span class="name">draggedItem</span> <span class="operator">=</span> <span class="name">itemComponent</span>.<span class="name">createObject</span>(<span class="name">window</span>, {&quot;image&quot;: <span class="name">paletteItem</span>.<span class="name">image</span>, &quot;x&quot;: <span class="name">posnInWindow</span>.<span class="name">x</span>, &quot;y&quot;: <span class="name">posnInWindow</span>.<span class="name">y</span>, &quot;z&quot;: <span class="number">3</span>});
          <span class="comment">// make sure created item is above the ground layer</span>
      } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">itemComponent</span>.<span class="name">status</span> <span class="operator">==</span> <span class="name">Component</span>.<span class="name">Error</span>) {
          <span class="name">draggedItem</span> <span class="operator">=</span> <span class="number">null</span>;
          <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;error creating component&quot;</span>);
          <span class="name">console</span>.<span class="name">log</span>(<span class="name">itemComponent</span>.<span class="name">errorString</span>());
      }
  }

  <span class="keyword">function</span> <span class="name">continueDrag</span>(mouse)
  {
      <span class="keyword">if</span> (<span class="name">draggedItem</span> <span class="operator">==</span> <span class="number">null</span>)
          <span class="keyword">return</span>;

      <span class="name">draggedItem</span>.<span class="name">x</span> <span class="operator">=</span> <span class="name">mouse</span>.<span class="name">x</span> <span class="operator">+</span> <span class="name">posnInWindow</span>.<span class="name">x</span> <span class="operator">-</span> <span class="name">startingMouse</span>.<span class="name">x</span>;
      <span class="name">draggedItem</span>.<span class="name">y</span> <span class="operator">=</span> <span class="name">mouse</span>.<span class="name">y</span> <span class="operator">+</span> <span class="name">posnInWindow</span>.<span class="name">y</span> <span class="operator">-</span> <span class="name">startingMouse</span>.<span class="name">y</span>;
  }

  <span class="keyword">function</span> <span class="name">endDrag</span>(mouse)
  {
      <span class="keyword">if</span> (<span class="name">draggedItem</span> <span class="operator">==</span> <span class="number">null</span>)
          <span class="keyword">return</span>;

      <span class="keyword">if</span> (<span class="name">draggedItem</span>.<span class="name">y</span> <span class="operator">&lt;</span> <span class="name">toolbox</span>.<span class="name">height</span>) { <span class="comment">//Don't drop it in the toolbox</span>
          <span class="name">draggedItem</span>.<span class="name">destroy</span>();
          <span class="name">draggedItem</span> <span class="operator">=</span> <span class="number">null</span>;
      } <span class="keyword">else</span> {
          <span class="name">draggedItem</span>.<span class="name">created</span> <span class="operator">=</span> <span class="number">true</span>;
          <span class="name">draggedItem</span> <span class="operator">=</span> <span class="number">null</span>;
      }
  }

</pre>
</div>
<!-- @@@dynamicscene/content/itemCreation.js -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
